package cn.edu.zjgsu.ito.service;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zjgsu.ito.model.TeacherModel;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class TeaTimetableService extends Service {
	/*
	 * 张虎涛
	 * 2016/06/01
	 */
	public List<Record> allTTT() {
		try {
			List<Record> list=Db.find(TeacherModel.dao.selectAllTttSql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Record> TTT(String tNo,String termValue) {
		try {
			List<Record> record=Db.find(TeacherModel.dao.selectTttSql,tNo,termValue);
			
			//助教课表
			List<Record> assistant = assistantTable(tNo);
			for(int i = 0;i < assistant.size();i++){
				record.add(assistant.get(i));
			}
			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    public List<Record> timeTable_term(String termValue) {
		try {
			List<Record> record=Db.find("select * from teacher_timetable where termValue=?",termValue);
			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Record> Term(String tNo) {
		try {
			List<Record> record=Db.find(TeacherModel.dao.selectTermSql,tNo);

			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public List<Record> AllTerm() {
		try {
			List<Record> record=Db.find(TeacherModel.dao.selectAllTermSql);

			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    
    public Record getTimeTable(String uuid) {
		try {
			Record record=Db.findFirst("select * from teacher_timetable where uuid=?",uuid);

			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Record> selectTTT(String termValue,String teacher,String course) {
		
		try {
			if( (!termValue.equals("0")) && (!course.equals("0")) &&(!teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and termValue=? and courseNo=? and tNo=?",termValue,course,teacher);
				return list;
			}else if( (termValue.equals("0")) && (!course.equals("0")) &&(!teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and courseNo=? and tNo=?",course,teacher);
				return list;
			}else if( (!termValue.equals("0")) && (course.equals("0")) &&(!teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and termValue=? and tNo=?",termValue,teacher);
				return list;
			}else if( (!termValue.equals("0")) && (!course.equals("0")) &&(teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and termValue=? and courseNo=? ",termValue,course);
				return list;
			}else if( (!termValue.equals("0")) && (course.equals("0")) &&(teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and termValue=? ",termValue);
				return list;
			}else if( (termValue.equals("0")) && (!course.equals("0")) &&(teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and courseNo=? ",course);
				return list;
			}else if( (termValue.equals("0")) && (course.equals("0")) &&(!teacher.equals("0")) ){
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and tNo=?",teacher);
				return list;
			}
			else{
				List<Record> list=Db.find("select * from test,teacher_info,course,course_end where course_end.testNo=test.testNo and test.tNo=teacher_info.tNo and test.courseNo=course.courseNo");
				return list;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<Record> assistantTable(String tNo){
		List<Record> lesson = new ArrayList<Record>();
		List<Record> temp = Db.find("select * from major_timetable");
		Record teacher = Db.findFirst("select * from teacher_info where tNo=?",tNo);
		String tName = teacher.getStr("tName");
		for(int i = 0;i < temp.size();i++){
			String[] nameStr = temp.get(i).getStr("assistant_tName").split(",");
			for(int j = 0;j < nameStr.length;j++){
				if(tName.equals(nameStr[j])){
					lesson.add(temp.get(i));
				}
			}
		}
		return lesson;
	}


}
